<template>
  <a-form
    ref="formRef"
    class="register-form-wrapper"
    layout="vertical"
    :model="form"
    :rules="rules"
  >
    <div class="register-form-title">欢迎注册</div>
    <a-tabs v-model:active-key="tabActiveKey" size="mini" animation>
      <a-tab-pane key="1" title="账号注册" destroy-on-hide>
        <a-form-item field="account" validate-trigger="blur" hide-label>
          <a-input
            v-model="form.account"
            autocomplete="account"
            placeholder="账号"
          >
            <template #prefix>
              <icon-user />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item field="password" validate-trigger="blur" hide-label>
          <a-input-password
            v-model="form.password"
            autocomplete="current-password"
            placeholder="请输入密码"
            allow-clear
          >
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>
        <a-form-item field="checkPassword" validate-trigger="blur" hide-label>
          <a-input-password
            v-model="form.checkPassword"
            autocomplete="current-check-password"
            placeholder="请输入重复密码"
            allow-clear
          >
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>
        <div class="login-link">
          已有账号? 点击
          <a-link href="/login">注册</a-link>
        </div>
      </a-tab-pane>
    </a-tabs>
    <a-button
      type="primary"
      style="margin: 32px 0 6px"
      long
      :loading="loading"
      @click="handleSubmit"
    >
      注册
    </a-button>
  </a-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import useLoading from '@/hooks/useLoading';
import { pick } from 'lodash';
import { Message } from '@arco-design/web-vue';
import { userRegisterUsingPost } from '@/service/backend/userController';
import { clearToken } from '@/utils/auth.ts';

const router = useRouter();
const formRef = ref();
const tabActiveKey = ref('1');
const { loading, setLoading } = useLoading();

const form = reactive({
  account: '',
  password: '',
  checkPassword: ''
});

const rules = {
  account: [{ required: true, message: '请输入正确账号' }],
  password: [
    { required: true, message: '请输入密码' },
    { minLength: 8, message: '密码至少8位' }
  ],
  checkPassword: [
    { required: true, message: '请输入重复密码' },
    {
      validator: (value, cb) => {
        if (value !== form.password) {
          cb('两次密码不匹配');
        } else {
          cb();
        }
      }
    }
  ]
};

const handleSubmit = () => {
  if (loading.value) return;

  formRef.value
    .validateField(['account', 'password', 'checkPassword'])
    .then(async res => {
      if (res) return;
      setLoading(true);
      try {
        const userInfoForm = pick(form, [
          'account',
          'password',
          'checkPassword'
        ]);

        const res = await userRegisterUsingPost({ ...userInfoForm });
        if (res.code !== 0) {
          throw new Error(res.message);
        }
        const { redirect, ...othersQuery } = router.currentRoute.value.query;
        router.push({
          name: (redirect as string) || 'Dashboard',
          query: {
            ...othersQuery
          }
        });
        Message.success('注册成功');
      } catch (error) {
        Message.error('注册失败,' + error.message);
        clearToken();
      } finally {
        setLoading(false);
      }
    });
};
</script>

<style lang="less" scoped>
.login-link {
  display: flex;
  justify-content: right;
  align-items: center;
}

.register-form {
  &-wrapper {
    width: 330px;
    padding: 24px 24px 12px;
    overflow: hidden;
    background-color: var(--color-bg-1);
    border: 1px solid var(--color-border-2);
    border-radius: var(--border-radius-large);
  }

  &-title {
    margin-bottom: 16px;
    font-size: 24px;
    font-weight: 500;
    line-height: 32px;
    color: var(--color-text-1);
  }

  &-actions {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    margin-top: 24px;
    margin-bottom: 6px;
  }

  &-register-btn {
    color: var(--color-text-3) !important;
  }

  &-more {
    justify-content: center;

    .arco-icon {
      font-size: 20px;
      cursor: pointer;
      transition: transform 0.2s ease;

      &:hover {
        transform: scale(1.3);
      }
    }
  }
}

:deep(.arco-tabs-content) {
  height: 195px;
}
</style>
